﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace MyFractals
{
    [StructLayout(LayoutKind.Sequential)]
    struct LASTINPUTINFO
    {
        // 设置结构体块容量  
        [MarshalAs(UnmanagedType.U4)]
        public int cbSize;
        // 捕获的时间  
        [MarshalAs(UnmanagedType.U4)]
        public uint dwTime;
    }

    public partial class Form1 : Form
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        [DllImport("user32.dll")]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);


        public float da1 = 0, da2 = 0, rp1 = 0, rp2 = 0, lw = 0;
        public Color bgcolor = Color.Black, linecolor = Color.White;

        private bool Drawing = false;
        private Form2 formdrawing = null;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            randomizeParameters();
        }

        private static long GetInactiveTime()
        {
            LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
            vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
            // 捕获时间  
            if (!GetLastInputInfo(ref vLastInputInfo))
                return 0;
            else
                return Environment.TickCount - (long)vLastInputInfo.dwTime;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }

            if (e.Button == MouseButtons.Right)
            {
                DrawFractal();
            }

        }

        private void DrawFractal()
        {
            if (Drawing)
            {
                if (formdrawing != null)
                {
                    formdrawing.Close();
                }
                Drawing = false;
            }

            da1 = 0; da2 = 0; rp1 = 0; rp2 = 0; lw = 0;
            if (
                float.TryParse(textBox1.Text, out da1) &&
                float.TryParse(textBox3.Text, out da2) &&
                float.TryParse(textBox2.Text, out rp1) &&
                float.TryParse(textBox4.Text, out rp2) &&
                float.TryParse(textBox5.Text, out lw)
                )
            {
                label5.Visible = false;
                Drawing = true;
                formdrawing = new Form2(da1, da2, rp1, rp2, lw, bgcolor, linecolor, trackBar1.Value, trackBar2.Value);
                formdrawing.Show();
            }
            else
            {
                label5.Visible = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                bgcolor = colorDialog1.Color;
                button3.BackColor = bgcolor;
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                linecolor = colorDialog1.Color;
                button4.FlatAppearance.BorderColor = linecolor;
            }
        }
        

        private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                randomizeParameters();
            }
            if (e.Button == MouseButtons.Middle)
            {
            }
            if (e.Button == MouseButtons.Right)
            {
                DrawFractal();
            }
        }

        private void randomizeParameters()
        {
            Random ran = new Random();
            textBox1.Text = (ran.Next(1, 3600) / 10.0).ToString();
            textBox2.Text = (ran.Next(1, 100)).ToString();
            textBox3.Text = (ran.Next(1, 3600) / 10.0).ToString();
            textBox4.Text = (ran.Next(1, 100)).ToString();
            textBox5.Text = (ran.Next(1, 30) / 10.0).ToString();
            bgcolor = Color.FromArgb(255, ran.Next(0, 255), ran.Next(0, 255), ran.Next(0, 255));
            linecolor = Color.FromArgb(255, ran.Next(0, 255), ran.Next(0, 255), ran.Next(0, 255));
            button3.BackColor = bgcolor;
            button4.FlatAppearance.BorderColor = linecolor;
            trackBar1.Value = ran.Next(1, 5);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            FilterTextBox(ref textBox1, e);
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            FilterTextBox(ref textBox2, e);
        }

        private void button_min_Click(object sender, EventArgs e)
        {
            Hide();
            notifyIcon1.Visible = true;
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            Show();
            notifyIcon1.Visible = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (GetInactiveTime() > 5 * 60 * 1000)
            {
                if (!Drawing)
                {
                    randomizeParameters();
                    DrawFractal();
                }
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            Hide();
            notifyIcon1.Visible = true;
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            FilterTextBox(ref textBox3, e);
        }
        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            FilterTextBox(ref textBox4, e);
        }
        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            FilterTextBox(ref textBox5 , e);
        }

        private void FilterTextBox(ref TextBox tb, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
                e.Handled = true;
            if ((int)e.KeyChar == 46)
            {
                if (tb.Text.Length <= 0)
                    e.Handled = true;
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(tb.Text, out oldf);
                    b2 = float.TryParse(tb.Text + e.KeyChar.ToString(), out f);
                    if (b2 == false)
                    {
                        if (b1 == true)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}

